﻿
Imports EstoqueDAL
Imports EstoqueBLL
Public Class PecaCAD
    Dim objBLL As New PECABLL
    Dim operacao As Char
    Public objcategoria As New CategoriaBLL
    Public objfabricante As New FabricanteBLL
    Public tipo As String
#Region "Eventos"
#Region "Busca Fabricante_click"
    Private Sub BBuscaFabricante_Click(sender As System.Object, e As System.EventArgs) Handles BBuscaFabricante.Click
        Dim objfab As New FabricanteCON
        objfab.tipo = "ret"
        objfab.ShowDialog()
        If (Not IsNothing(objfab.objfabri)) Then
            objfabricante = objfab.objfabri
            tfabricante.Text = objfabricante.fab_descricao
        End If
    End Sub
#End Region

#Region "Busca Categoria_click"
    Private Sub BBuscaCategoria_Click(sender As System.Object, e As System.EventArgs) Handles BBuscaCategoria.Click
        'buscar categoria e gravar
        Dim objcat As New CategoriaCON
        objcat.tipo = "ret"
        objcat.ShowDialog()
        If (Not IsNothing(objcat.objcate)) Then
            objcategoria = objcat.objcate
            tcategoria.Text = objcategoria.cat_descricao
        End If
    End Sub
#End Region

#Region "Evento key press"
    Private Sub eventoKeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TEstMinimo.KeyPress, tvunitario.KeyPress, TPVenda.KeyPress, TPCusto.KeyPress, TMC.KeyPress, TMargem.KeyPress, TEstMaximo.KeyPress, TEstAtual.KeyPress, tdesconto.KeyPress, TCodBarras.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
#End Region

#Region "Form Closing"
    Private Sub CategoriaCAD_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        limpar()
    End Sub
#End Region

#Region "Load"
    Private Sub CategoriaCAD_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'sta_codigo: 1- ativo, 2 inativo
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo=1 or sta_codigo=2", "sta_descricao")
        objconexao.carregaCombo(CBClassFiscal, "Estoque.Gen_ClaFiscal", "clf_codigo", "clf_descricao", "sta_codigo=1", "clf_descricao")
        If (Not IsNothing(lcodigo.Text) And lcodigo.Text.Trim <> "") Then
            objBLL = New PECABLL(Val(lcodigo.Text))
            carregacampos()
            operacao = "A"
            lcodigo.Visible = True
        Else : operacao = "I"
        End If
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub PecaCAD_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
#End Region

#Region "Salvar"
    Private Sub TSBGravar_Click(sender As System.Object, e As System.EventArgs) Handles TSBGravar.Click
        Try
            'carregar campos
            objBLL = New PECABLL(Val(lcodigo.Text), Val(objBLL.getproduto(Val(lcodigo.Text))), CBClassFiscal.SelectedValue, Val(objfabricante.fab_codigo),
                 CBStatus.SelectedValue, objcategoria.cat_codigo, TCodBarras.Text, Val(TEstMinimo.Text),
                 Val(TEstMaximo.Text), Val(TEstAtual.Text), 0, 0, Trim(TCodPeca.Text), Decimal.Parse(tvunitario.Text),
                 Decimal.Parse(TMC.Text), Decimal.Parse(TMargem.Text), StrConv(RTrim(LTrim(tabreviacao.Text)), VbStrConv.ProperCase), Trim(TCodFabricante.Text),
                StrConv(LTrim(RTrim(TDescricao.Text)), VbStrConv.ProperCase), Decimal.Parse(TPCusto.Text), Decimal.Parse(TPVenda.Text), Decimal.Parse(tdesconto.Text))
            If operacao = "I" Then
                If (objBLL.inseriralterar(objusuarios.Codigo)) Then
                    MessageBox.Show("Peça inserida com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("A peça não pôde ser inserida", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            Else
                If (objBLL.inseriralterar(objusuarios.Codigo)) Then
                    MessageBox.Show("Peça alterada com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("A peça não pôde ser alterada", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region
#End Region

#Region "Métodos"
#Region "Limpar"
    Public Sub limpar()
        Me.TCodBarras.Clear()
        Me.CBStatus.SelectedValue = 0
        Me.TDescricao.Clear()
        Me.tabreviacao.Clear()
        Me.TCodFabricante.Clear()
        Me.TCodPeca.Clear()
        Me.objfabricante = Nothing
        Me.TEstMinimo.Clear()
        Me.TEstMaximo.Clear()
        Me.TEstAtual.Clear()
        Me.objcategoria = Nothing
        Me.CBClassFiscal.SelectedValue = 0
        Me.TPCusto.Clear()
        Me.TMC.Clear()
        Me.tvunitario.Clear()
        Me.tdesconto.Clear()
        Me.TMargem.Clear()
        Me.TPVenda.Clear()
        Me.objBLL = Nothing
        Me.lcodigo.Text = Nothing
    End Sub
#End Region

#Region "Carregar Campos"
    Public Sub carregacampos()

        Me.TCodBarras.Text = objBLL.pec_codbarra
        Me.CBStatus.SelectedValue = objBLL.sta_codigo
        Me.TDescricao.Text = objBLL.pro_descricao
        Me.tabreviacao.Text = objBLL.pec_abreviacao
        Me.TCodFabricante.Text = objBLL.pec_codigofabricante
        Me.TCodPeca.Text = objBLL.pec_original
        Me.objfabricante = New FabricanteBLL(objBLL.fab_codigo)
        Me.tfabricante.Text = objfabricante.fab_descricao
        Me.TEstMinimo.Text = objBLL.pec_qtdemin
        Me.TEstMaximo.Text = objBLL.pec_qtdemax
        Me.TEstAtual.Text = objBLL.pec_qtde
        Me.objcategoria = New CategoriaBLL(objBLL.cat_codigo)
        Me.tcategoria.Text = objcategoria.cat_descricao
        Me.CBClassFiscal.SelectedValue = objBLL.clf_codigo
        Me.TPCusto.Text = objBLL.pro_precocusto
        Me.TMC.Text = objBLL.pec_mcontrib
        Me.tvunitario.Text = objBLL.pec_valorunitario
        Me.tdesconto.Text = objBLL.pro_desconto
        Me.TMargem.Text = objBLL.pec_margem
        Me.TPVenda.Text = objBLL.pro_precovenda
        Me.lcodigo.Text = objBLL.pec_codigo
    End Sub
#End Region
#End Region





End Class